package com.zhang.yygh.user.service.impl;

import com.atguigu.yygh.model.user.Patient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.yygh.cmn.client.DictFeignClient;
import com.zhang.yygh.user.mapper.PatientMapper;
import com.zhang.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-03-14
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;

    //修改就诊人第一步：先查询
    @Override
    public Patient getPatientInfo(Long pid) {
        Patient patient = baseMapper.selectById(pid);
        this.packagePatient(patient);
        return patient;
    }

    //查询就诊人
    @Override
    public List<Patient> getPatientList(Long userId) {
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(queryWrapper);
        //获取到就诊人集合后，要将信息中的省市区编号转换为具体的文字
        patientList.parallelStream().forEach(item->packagePatient(item));
        return patientList;
    }


    //将省市区编号转化为具体的文字
    private void packagePatient(Patient patient) {
        String provinceCode = patient.getProvinceCode(); //省
        String cityCode = patient.getCityCode(); //市
        String districtCode = patient.getDistrictCode(); //区

        //省市区的地址
        String provinceAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(provinceCode));
        String cityAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(cityCode));
        String districtAddress = dictFeignClient.getAddressNameByValue(Long.valueOf(districtCode));

        //证件类型
        String certificatesTypeString = dictFeignClient.getAddressNameByValue(Long.valueOf(patient.getCertificatesType()));

        patient.getParam().put("provinceString", provinceAddress);
        patient.getParam().put("cityString", cityAddress);
        patient.getParam().put("districtString", districtAddress);
        //详细地址
        patient.getParam().put("fullAddress", provinceAddress + cityAddress + districtAddress + patient.getAddress());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);

    }
}
